package org.tinygame.herostory.cmdHandler;

import io.netty.channel.ChannelHandlerContext;
import org.tinygame.herostory.msg.GameMsgProtocol;
import org.tinygame.herostory.rank.RankItem;
import org.tinygame.herostory.rank.RankService;

import java.util.Collections;

/**
 * 获取排行榜指令处理器
 * @author shkstart
 * @create 2022-05-08 19:38
 */
public class GetRankCmdHandler implements ICmdHandler<GameMsgProtocol.GetRankCmd>{

    @Override
    public void handle(ChannelHandlerContext ctx, GameMsgProtocol.GetRankCmd cmd) {
        if(null==ctx||null==cmd){
            return;
        }

        RankService.getInstance().getRank((rankItemList)->{
            if(null==rankItemList){
                rankItemList= Collections.emptyList();
            }

            GameMsgProtocol.GetRankResult.Builder resultBuilder=GameMsgProtocol.GetRankResult.newBuilder();

            for(RankItem rankItem:rankItemList){
                GameMsgProtocol.GetRankResult.RankItem.Builder rankItemBuilder=
                        GameMsgProtocol.GetRankResult.RankItem.newBuilder();
                //构建rankItemBuilder
                rankItemBuilder.setRankId(rankItem.rankId);
                rankItemBuilder.setUserId(rankItem.userId);
                rankItemBuilder.setUserName(rankItem.userName);
                rankItemBuilder.setHeroAvatar(rankItem.heroAvatar);
                rankItemBuilder.setWin(rankItem.win);

                //将每个rankItemBuilder都加入resultBuilder
                resultBuilder.addRankItem(rankItemBuilder);
            }

            //构建结果并发送
            GameMsgProtocol.GetRankResult newResult=resultBuilder.build();
            ctx.writeAndFlush(newResult);

            return null;
        });
    }
}
